package j20251013;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permute(int[] nums) {
        ret=new ArrayList<>();
        path=new ArrayList<>();
        check=new boolean[nums.length];
        dfs(nums);
        return ret;
    }

    public void dfs(int[] nums){
        if(path.size()==nums.length){
            ret.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<nums.length;i++){
            if(check[i]==false){
                path.add(nums[i]);
                check[i]=true;
                dfs(nums);
                //回溯
                path.remove(path.size()-1);
                check[i]=false;
            }
        }
    }

    public List<List<Integer>> subsets(int[] nums) {
        ret=new ArrayList<>();
        path=new ArrayList<>();
        dfs(nums,0);
        return ret;
    }

    public void dfs(int[] nums,int pos){
        ret.add(new ArrayList<>(path));
        for(int i=pos;i<nums.length;i++){
            path.add(nums[i]);
            dfs(nums,i+1);
            //回溯
            path.remove(path.size()-1);
        }
    }
}
